import { Singleton } from '../../framework/common/Singleton';
import { RemoteMgr } from './RemoteMgr';
import { ServerEvents } from '../../../../shares/sharesApp';
import { EventEmitter } from '../../framework/common/EventEmitter';
import { PlayerMgr } from './PlayerMgr';
import { GameMgr } from './GameMgr';
import { RoleType } from '../const/RoleType';

export class DialogMgr extends Singleton<DialogMgr>() {
    /**
     * 初始化 / Initialize
     */
    public start(): void {
        this.bindEvents();
    }
    /**
     * 绑定事件 / Bind events
     */
    bindEvents() {
        RemoteMgr.instance.onServerEvent((event) => {
            if (event.args.type === ServerEvents.DIALOG_END) {
                EventEmitter.instance.emit('dialogEnd', event.entity);
                console.log('(Server) Client dialogEnd');
            }
        });
    }
    /**
     * 获取NPC对话
     * @param id NPC唯一标识
     * @param entity 玩家实体对象 / Player entity object
     * @param event 交互事件
     */
    public dialog(
        id: string,
        entity: GamePlayerEntity,
        event: GameInteractEvent
    ) {
        // const player = PlayerMgr.instance.getPlayer(entity.player.userId);
        switch (id) {
            case 'witch':
                this.witch(entity);
                break;
            case 'pumpkin':
                this.pumpkin(entity, event.targetEntity);
                break;
        }
    }

    witch(entity: GamePlayerEntity) {
        GameMgr.instance.onPlayerJoin(entity.player.userId);
    }

    pumpkin(entity: GamePlayerEntity, targetEntity: GameEntity) {
        const player = PlayerMgr.instance.getPlayer(entity.player.userId);
        if (player) {
            player.roleType = RoleType.Pumpkin;
        }
        targetEntity.destroy();
    }
}
